package com.uchengwang.ui.base_ui.basefm;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.MainActivity;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.cm_ui.me.adapter.FmMeAdapter;
import com.uchengwang.ui.cm_ui.me.bean.FmMeBean;
import com.uchengwang.ui.cm_ui.me.presenter.HttpMeManager;
import com.uchengwang.ui.cm_ui.me.presenter.HttpUserManager;
import com.uchengwang.ui.cm_ui.me.ui.MyPerInfoActivity;
import com.uchengwang.ui.cm_ui.me.ui.MySettingActivity;
import com.uchengwang.ui.cm_ui.ucity.ui.QRCodeShowActivity;
import com.uchengwang.ui.cm_ui.ucity.ui.UsersActivity;
import com.uchengwang.ui.utils.SPUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by U城网 on 2017/2/28.
 */

/**
 * 我的模块
 */
public class FragmentUMe extends BaseFragment implements View.OnClickListener{
    private View view;
    private ImageView bg, left_image, fmMe_head, fmMe_editor_image, fmMe_erweima;//背景图
    private TextView fmMe_name, fmMe_sign, fmMe_friendNum, fmMe_fansNum, fmMe_attenNum;
    private TextView fmMe_fans_noread;
    //个人显示未读消息
    private RelativeLayout fmMe_bg_rl;

    public static boolean isRefresh = false;//当有新粉丝 新关注来时, 刷新

    private ListView fmMe_lv;//显示对应类别的信息
    private FmMeAdapter adapter;
    private List<FmMeBean> adapterList = new ArrayList<>();
    private long reFreshTime;//刷新时间

    private LinearLayout fmMe_friend_linear, fmMe_fansNum_linear, fmMe_attenNum_linear;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setLayoutId(R.layout.fragment_me);
        view = super.onCreateView(inflater, container, savedInstanceState);

        initView(view);

        return view;
    }

    private void initView(View view){
        left_image = view.findViewById(R.id.fmMe_left_image);

//        fmMe_bg_rl = view.findViewById(R.id.fmMe_bg_rl);
//        fmMe_bg_rl.setOnClickListener(this);

//        fmMe_editor_image = view.findViewById(R.id.fmMe_left_image);
//        fmMe_editor_image.setOnClickListener(this);

        fmMe_head = view.findViewById(R.id.fmMe_head);
        fmMe_head.setOnClickListener(this);
        fmMe_name = view.findViewById(R.id.fmMe_name);

        fmMe_friend_linear = view.findViewById(R.id.fmMe_friend_linear);
        fmMe_friend_linear.setOnClickListener(this);

        fmMe_fansNum_linear = view.findViewById(R.id.fmMe_fansNum_linear);
        fmMe_fansNum_linear.setOnClickListener(this);
        fmMe_attenNum_linear = view.findViewById(R.id.fmMe_attenNum_linear);
        fmMe_attenNum_linear.setOnClickListener(this);
        fmMe_friendNum = view.findViewById(R.id.fmMe_friendNum);
        fmMe_fansNum = view.findViewById(R.id.fmMe_fansNum);
        fmMe_attenNum = view.findViewById(R.id.fmMe_attenNum);
        fmMe_fans_noread = view.findViewById(R.id.fmMe_fans_noread);

        fmMe_erweima = view.findViewById(R.id.fmMe_erweima);
        fmMe_erweima.setOnClickListener(this);

//        fmMe_sign = view.findViewById(R.id.fmMe_sign);

//        bg = view.findViewById(R.id.fmMe_bg);
        fmMe_lv = view.findViewById(R.id.fmMe_lv);
        fmMe_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                click(adapterList.get(position).getName());
            }
        });

        adapter = new FmMeAdapter(getContext());
        fmMe_lv.setAdapter(adapter);

        //设置顶部的背景图
//        ImageManager.getInstance().loadImage(this.getContext(), bg, R.drawable.image_person_bg, ImageManager.ImageType.NARMOL);

        addItemView();
    }

    private void addItemView(){
        initAdapter();
    }

    /**
     * 初始化加载数据类别
     * 根据不同身份 状态加载不同的数据类型
     * */
    private void initAdapter(){
        adapterList.clear();
        switch (UChengApplication.userRole){
            case BaseConfig.STUDENT_ROLE:
            case 0:
            case BaseConfig.UNI_ROLE:
            case BaseConfig.EDITOR_ROLE:
            case BaseConfig.ZONG_ROLE:
                adapterList.add(new FmMeBean(0, "发布", false));
//                adapterList.add(new FmMeBean(R.drawable.my_noticy_icon, "通知", false));
//                adapterList.add(new FmMeBean(R.drawable.my_collect_image, "收藏", false));
//                adapterList.add(new FmMeBean(R.drawable.my_service_icon, "组织", false));
//                adapterList.add(new FmMeBean(R.drawable.my_service_icon, "社团", false));
                adapterList.add(new FmMeBean(0, "票劵", false));

                adapterList.add(new FmMeBean(0, "资料", false));
//                adapterList.add(new FmMeBean(R.drawable.my_service_icon, "服务", false));
                adapterList.add(new FmMeBean(0, "设置", false));

                adapter.setData(adapterList);
                break;
        }
    }

    //更新自己的信息
    private void initMyInfoView(){
        if (UCommonUtil.getUtil().isLogin()){
            fmMe_name.setText(UChengApplication.nickName);
            String sign = SPUtils.getPreferences(getContext()).getString(BaseConfig.USER_SIGN);
//            if (TextUtils.isEmpty(sign)){
//                fmMe_sign.setText("暂无个性签名");
//            }else {
//                fmMe_sign.setText(sign);
//            }

            if (!TextUtils.isEmpty(UChengApplication.userAvatar)){
                ImageManager.getInstance().loadImage(FragmentUMe.this.getContext(), fmMe_head, UChengApplication.userAvatar, ImageManager.ImageType.CIRCLE);
            }else {
                ImageManager.getInstance().loadImage(FragmentUMe.this.getContext(), fmMe_head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
            }
        }else {
            ImageManager.getInstance().loadImage(FragmentUMe.this.getContext(), fmMe_head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
            fmMe_name.setText("点击登录");
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        if (UChengApplication.isAccountChange){
            addItemView();
        }
        initMyInfoView();

        if (UCommonUtil.getUtil().isLogin() && (System.currentTimeMillis()- reFreshTime)/1000/60 >5){
            getUnReadMsg();
            getMyUsers();
            reFreshTime = System.currentTimeMillis();
        }

    }


    //获取我的好友数量
    public void getMyUsers(){
        reFreshTime = System.currentTimeMillis();
        HttpMeManager.getPresenter().checkMyCount(getActivity(), new HttpMeManager.CheckMyCountListener() {
            @Override
            public void onResult(int fans, int follow, int friend) {
                    fmMe_fansNum.setText(fans+"");
                    fmMe_attenNum.setText(follow+"");
                    fmMe_friendNum.setText(friend+"");

                }
        });

        //获取网络数据 新粉丝数量
        HttpUserManager.getManager().getMyFans(getActivity(), false, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                if (total >0 ){
                    fmMe_fans_noread.setVisibility(View.VISIBLE);
                }
            }
        });
    }



    /**
     * 获取未读消息
     * */
    public void getUnReadMsg(){
        //查看我的新票劵
        HttpUserManager.getManager().getMyTickets(getActivity(), true, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                if (total >0 ){
                    updateAdapter("发布", true);
                    ((MainActivity)FragmentUMe.this.getActivity()).showDot(2);
                }
            }
        });

        //获取发布未读信息
//        UUHttpManager.getManager().getCmtMsg(getActivity(), 1, 1, false, new UUHttpManager.CmtMsgListener() {
//            @Override
//            public void onResult(int stutas, int totalPages, List<CmtmsgBean> listBean) {
//                if ( totalPages >0 && release_noRead != null){
//                    if (listBean != null && listBean.size() >0){
//                        CmtmsgBean b = listBean.get(0);
//                        if (!b.isRead()){
//                            release_noRead.setVisibility(View.VISIBLE);
//                            ((MainActivity)FragmentUMe.this.getActivity()).showDot(3);
//                        }
//
//                    }
//
//                }
//            }
//        });
    }

    @Override
    public void onStop() {
        super.onStop();
        isRefresh = false;
        BaseConfig.changeGroup = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * adapter点击调的方法
     * */
    private void click(String name){
        if (UCommonUtil.getUtil().isNeedLogin(getActivity(), false)){
            return;
        }

        updateAdapter(name, false);
        Intent intent = null;
        switch (name){
            case "发布":
                intent = new Intent(getContext(), LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/user/myshare");
                intent.putExtra(BaseConfig.URL_TITLE, "我的发布");
                intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                break;
//            case "通知":
//                intent = new Intent(getContext(), SystemNotifyActivity.class);
//                break;
//            case "收藏":
//                intent = new Intent(getContext(), LoadCycleWebHtmlActivity.class);
//                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/user/collect");
//                intent.putExtra(BaseConfig.URL_TITLE, "我的收藏");
//                break;
//            case "组织":
//                UCommonUtil.getUtil().showToast(getContext(), "敬请期待");
//                intent = new Intent(getContext(), MyAccountActivity.class);
//                intent = new Intent(getContext(), MyAccountActivity.class);
//                break;
            case "资料":
                intent = new Intent(getContext(), MyPerInfoActivity.class);
//                intent = new Intent(getContext(), PlayVideoCustomActivity.class);
//                intent.putExtra(BaseConfig.VIDEO_ID, "3156fc1e0af04ebfb590c734b4a3dd75");
                break;
            case "票劵":
                intent = new Intent(getContext(), LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/user/ticket");
                intent.putExtra(BaseConfig.URL_TITLE, "票劵");
                intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);

                //重置我的新票劵数据
//                HttpUserManager.getManager().getMyTickets(getActivity(), true, new OnResultListener() {
//                    @Override
//                    public void onResult(int stutas, String msg, int total, String data) {
//                    }
//                });
                break;
            case "设置":
                intent = new Intent(getContext(), MySettingActivity.class);
                break;
        }
        if (intent != null){
            startActivity(intent);
        }
        hideTabDot();
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        if (UCommonUtil.getUtil().isNeedLogin((MainActivity)getContext(), false)){
            return;
        }

        switch (view.getId()){
            case R.id.fmMe_head:
                //头像点击
                List<String> headUrl = new ArrayList<>();
                headUrl.add(UChengApplication.userAvatar);
                UCommonUtil.getUtil().startImagePagerActivity(getContext(), OSSManager.OSS_HEAD, headUrl, 0, false);

                break;
//            case R.id.fmMe_left_image:
//                intent = new Intent(this.getContext(), MyPerInfoActivity.class);
//                break;

            case R.id.fmMe_bg_rl://我的信息界面
//                intent = new Intent(this.getContext(), MainPageActivity.class);
//                intent.putExtra(BaseConfig.ID, UCommonUtil.getUtil().getId());
//                intent = new Intent(getContext(), LoadCycleWebHtmlActivity.class);
//                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/user/"+UCommonUtil.getUtil().getId());
                break;
            case R.id.fmMe_friend_linear://我的好友
                reFreshTime =0;//为了返回时 能刷新我的关系数量
                intent = new Intent(getContext(), UsersActivity.class);
                intent.putExtra(BaseConfig.ID, UCommonUtil.getUtil().getId());
                intent.putExtra(BaseConfig.TYPE, 3);
                break;

            case R.id.fmMe_fansNum_linear://我的粉丝
                reFreshTime =0;
                intent = new Intent(getContext(), UsersActivity.class);
                intent.putExtra(BaseConfig.ID, UCommonUtil.getUtil().getId());
                intent.putExtra(BaseConfig.TYPE, 1);
                fmMe_fans_noread.setVisibility(View.GONE);
                break;

            case R.id.fmMe_attenNum_linear://我的关注
                reFreshTime =0;
                intent = new Intent(getContext(), UsersActivity.class);
                intent.putExtra(BaseConfig.ID, UCommonUtil.getUtil().getId());
                intent.putExtra(BaseConfig.TYPE, 2);
                break;

            case R.id.fmMe_erweima://二维码
                intent = new Intent(getContext(), QRCodeShowActivity.class);
                intent.putExtra(BaseConfig.TYPE, 1);
                intent.putExtra(BaseConfig.ID, UCommonUtil.getUtil().getId());
                break;

//            case R.id.fmMe_uni_feedback://反馈
//                intent = new Intent(this.getContext(), FeedbackItemActivity.class);
//                break;
//            case R.id.fmMe_uni_feedback_check://查看反馈
//                intent = new Intent(getContext(), CmtMsgAndFeedbackActivity.class);
//                intent.putExtra(BaseConfig.TYPE, 3);
//                break;
//            case R.id.fmMe_uni_club://提交社团
//                intent = new Intent(getContext(), SubmitClubActivity.class);
//                break;
//            case R.id.fmMe_uni_recomment://uni分享到微信
//                ShareToWxUtils.getUtils(this.getContext()).share(this.getContext(), left_image, "http://uchengwang.com/m/", "U城", "U城-大学不止路过");
//                break;
        }

        if (intent != null){
            startActivity(intent);
        }
    }

    //提供方法 更新小红点  isNew true显示小红点 false隐藏
    public void updateAdapter(String title, boolean isNew){
        if (!TextUtils.isEmpty(title)){
            List <FmMeBean> meBeanList = adapter.getData();
            for (FmMeBean fmMeBean : meBeanList){
                if (title.equals(fmMeBean.getName())){
                    fmMeBean.setNew(isNew);
                    break;
                }
            }
            adapter.setData(meBeanList);
        }
    }

    //隐藏tab小红点
    private void hideTabDot(){
        List <FmMeBean> meBeanList = adapter.getData();
        for (FmMeBean fmMeBean : meBeanList){
            if (fmMeBean.isNew()){
                return;//只有还有就结束此方法
            }
        }
        ((MainActivity)getActivity()).hideDot(2);
    }

}
